const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,

  css: {
    loaderOptions: {
      stylus: {
        'resolve url': true,
        'import': []
      }
    }
  },

  pluginOptions: {
    'cube-ui': {
      postCompile: true,
      theme: false
    }
  },

  configureWebpack: {
    // devServer: {
    //   proxy: {
    //     '/api': {
    //       target: 'http://localhost:3000',
    //       changeOrigin: true,          
    //     }
    //   }
    // }
    devServer: {    
      proxy: {
            '/api': {
              target: 'http://localhost:3000',
              changeOrigin: true,          
            } 
      }, 
      //mock数据:
      onBeforeSetupMiddleware(devServer){
        function auth(req,res,next){
          if(req.headers.token){
            next()
          }else{
            res.sendStatus(401)
          }
        }

        devServer.app.get("/api/login",(req,res)=>{
          const {username,password} = req.query
          if(username === "admin" && password === "123456"){
            res.json({
              code:200,
              message:"登录成功",
              token:"1234567890"
            })
          }else{
            res.status(401).json({
              code:401,
              message:"用户名或密码错误"
            })
          }          
        }),

        

        devServer.app.get("/api/userinfo",auth,(req,res)=>{
          res.json({
            code:200,data:{name:"admin"}
          })
        }),


        devServer.app.get("/api/goods",(req,res)=>{
          res.json({
            code:1,
            slider:[
              {
                id:1,
                img:"/img/01.jpg"
              },
              {
                id:2,
                img:"/img/02.png"
              },
              {
                id:3,
                img:"/img/03.jpg"
              }
            ],
            data:{
              fe:[
                {
                  id:1,
                  title:"Vue",
                  img:"/img/04.webp",
                  price:"100",
                  count:100
                },
                {
                  id:2,
                  title:"React",
                  img:"/img/05.webp",
                  price:"200",
                  count:90
                },
                {
                  id:3,
                  title:"Angular",
                  img:"/img/06.webp",
                  price:"300",
                  count:80
                }
              ],
              python:[
                {
                  id:4,
                  title:"Python",
                  img:"/img/python.png",
                  price:"400",
                  count:70
                },
                {
                  id:5,
                  title:"Django",
                  img:"/img/django.png",
                  price:"500",
                  count:60
                },
              ],
              java:[
                {
                  id:6,
                  title:"Java",
                  img:"/img/java.png",
                  price:"600",
                  count:50
                },
                {
                  id:7,
                  title:"Spring",
                  img:"/img/spring.png",
                  price:"700",
                  count:40
                }
              ]
            },
            keys:["fe","python","java"]
          })
        })
      }
    }
  }
})
